<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class ModelePDO
{    
    // <editor-fold defaultstate="collapsed" desc="Champs"> 
    //Attributs utiles pour la connexion
    protected static $serveur = MySqlConfig::SERVEUR;
    protected static $base = MySqlConfig::BASE;
    protected static $utilisateur = MySqlConfig::UTILISATEUR;
    protected static $passe = MySqlConfig::PASSE;
    
    //Attributs utiles pour la manipulation PDO de la BDD
    protected static $pdoCnxBase = null;
    protected static $pdoStResults = null;
    protected static $requete = '';
    protected static $resultat = null;
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Méthodes"> 
    public static function seConnecter()
    {
        try
        {
            //Appel du Constructeur
            self::$pdoCnxBase = new PDO('mysql:host='.self::$serveur.';dbname='.self::$base,self::$utilisateur,self::$passe);
            self::$pdoCnxBase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            self::$pdoCnxBase->query('SET CHARACTER SET utf8');
        }
        catch (Exception $e)
        {
            echo 'Erreur : ' . $e->getMessage() . '<br/>'; //Methodes de la classe Exception
            echo 'Code : '.$e->getCode();
            die(); //Met fin au Script 
        }
    }

    private static function executerRequete()
    {
        try
        {
            self::$pdoStResults->execute();
        }
        catch (Exception $e)
        {
            echo 'Erreur : ' . $e->getMessage() . '<br/>'; //Methodes de la classe Exception
            echo 'Code : '.$e->getCode();
            die(); //Met fin au Script
        }
    }
    
    public static function seDeconnecter()
    {
        self::$pdoCnxBase = null;
    }
    
    public static function getLesTuples($table)
    {
        self::seConnecter();
        self::$requete = "SELECT * FROM " . $table;
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();
        return self::$resultat;
    }
    
    public static function getLesTuplesLimit($table, $debut, $nbTuples)
    {
        self::seConnecter();
        self::$requete = "SELECT * FROM " . $table . " LIMIT " . $debut . ", " . $nbTuples;
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();
        return self::$resultat;
    }
    
    public static function getLesTuplesOrderBy($table, $colonneOrder, $typeOrder = null)
    {
        $sqlOrder = "ORDER BY $colonneOrder ASC";
        if ($typeOrder != null)
        {
            $sqlOrder = "ORDER BY $colonneOrder $typeOrder";
        }
        self::seConnecter();
        self::$requete = "SELECT * FROM $table $sqlOrder";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();
        return self::$resultat;
    }
    
    public static function getPremierTupleByChamp($table, $nomChamp, $valeurChamp)
    {
        self::seConnecter();
        self::$requete = "SELECT * FROM $table WHERE $nomChamp = :value";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->bindValue('value', $valeurChamp);
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);        
        self::$pdoStResults->closeCursor();
        
        return self::$resultat;
    }
    
    public static function getLesTuplesByChampLike($table, $nomChamp, $valeurChamp)
    {
        self::seConnecter();
        self::$requete = "SELECT * FROM $table WHERE $nomChamp LIKE :value";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->bindValue('value', "%$valeurChamp%");
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);        
        self::$pdoStResults->closeCursor();
        
        return self::$resultat;
    }
    
    public static function getLesTuplesByChampBetween($table, $nomChamp, $debut, $fin)
    {
        self::seConnecter();
        self::$requete = "SELECT * FROM $table WHERE $nomChamp BETWEEN :debut AND :fin";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->bindValue('debut', $debut);
        self::$pdoStResults->bindValue('fin', $fin);
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);        
        self::$pdoStResults->closeCursor();
        
        return self::$resultat;
    }
    
    public static function getLesTuplesByChamp($table, $nomChamp, $valeurChamp)
    {
        self::seConnecter();
        self::$requete = "SELECT * FROM $table WHERE $nomChamp = :value";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->bindValue('value', $valeurChamp);
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);        
        self::$pdoStResults->closeCursor();
        
        return self::$resultat;
    }
           
    public static function deleteTuplesByChamp($table, $nomChamp, $valeurChamp)
    {
        self::seConnecter();
        self::$requete = "DELETE FROM $table WHERE ($nomChamp = :value)";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->bindValue('value', $valeurChamp);
        self::$pdoStResults->execute();
        self::seDeconnecter();
    }
    
    public static function getNbTuples($table)
    {
        self::seConnecter();
        self::$requete = "SELECT COUNT(*) AS nb FROM " . $table;
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();
        return self::$resultat->nb;
    }
    
    public static function getMaxTupleByChamp($table, $nomChamp)
    {
        self::seConnecter();
        self::$requete = "SELECT MAX($nomChamp) AS nb FROM " . $table;
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::seDeconnecter();
        self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();
        return self::$resultat->nb;
    }

    //Insère un tuple dans une table avec les champs passés dans un tableau
    public static function insertTuple($table, $lesValues)
    {
        self::seConnecter();
        self::$requete = "INSERT INTO $table VALUES (";
        //Tant que $i n'est pas égal au dernier élément du tableau
        for ($i = 1; $i != count($lesValues); $i++)
        {
            self::$requete .= ":champ$i,";
        }
        //Pour le dernier élément on ferme la parenthèse
        self::$requete .= ":champ$i)";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        //On remplace les champs par un bindValue pour sécuriser la requête
        for ($i = 1; $i <= count($lesValues); $i++)
        {
            self::$pdoStResults->bindValue("champ$i",$lesValues[$i - 1]);
        }
        self::$pdoStResults->execute();
        self::seDeconnecter();
    }

    //Modifie les champs voulus des tuples avec les valeurs associées, en fonction de la condition donnée
    public static function updateTuplesByChamp($table, $nomsChampsModif, $valuesChampsModif, $nomChampWhere, $valueChampWhere)
    {
        self::seConnecter();
        self::$requete = "UPDATE $table SET ";
        //Tant que $i n'est pas égal au dernier élément du tableau
        for ($i = 0; $i != count($nomsChampsModif)-1; $i++)
        {
            self::$requete .= "$nomsChampsModif[$i] = :$nomsChampsModif[$i],";
        }
        //Au dernier élément, on rajoute la condition
        self::$requete .= "$nomsChampsModif[$i] = :$nomsChampsModif[$i] WHERE ($nomChampWhere = :$nomChampWhere)";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        //On remplace les champs par un bindValue pour sécuriser la requête
        for ($i = 0; $i <= count($valuesChampsModif)-1; $i++)
        {
            self::$pdoStResults->bindValue("$nomsChampsModif[$i]",$valuesChampsModif[$i]);
        }
        self::$pdoStResults->bindValue($nomChampWhere, $valueChampWhere);
        self::$pdoStResults->execute();
        self::seDeconnecter();
    }
    
    public static function genererClePrimaire($table, $nomChamp)
    {
        $idMax = self::getMaxTupleByChamp($table, $nomChamp);
        if ($idMax != null)
        {
            return $idMax + 1;
        }
        else
        {
            return 1;
        }
    }
    // </editor-fold>
}
